package com.yuju.controller.pub;

import com.yuju.controller.base.BaseController;
import com.yuju.roomStatus.model.RoomStatusModel;
import com.yuju.roomStatus.service.IRoomStatusService;
import com.yuju.vo.rpt.BusinessDailyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 类名：RoomStatusPrintController  <br />
 *
 * 功能：
 *
 * @author : jiangp <br />
 * @Date : 2018年2月5日 下午2:58:28  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("roomStatusPrint")
public class RoomStatusPrintController extends BaseController<RoomStatusModel, RoomStatusModel> {
    @Autowired
    private IRoomStatusService roomStatusService;

    /**
     * 功能：导出房态打印<br/>
     *
     * @author jiaokn
     * @Date 2018年2月1日 下午3:00:48 <br/>
     */
    @RequestMapping(value = "/expRoomStatusPrintExcel")
    public void expRoomStatusPrintExcel(RoomStatusModel roomStatusModel) throws Exception {
        roomStatusModel.setBeginDay(new Date());
        roomStatusModel.setEndDay(new Date());
        HttpServletResponse response = getResponse();
        String excelFileName = "";
        InputStream excelStream = null;
        byte[] bytes = roomStatusService.expRoomStatusPrint(roomStatusModel);
        ByteArrayInputStream is = new ByteArrayInputStream(bytes);
        excelStream = is; // 文件流
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        String date = sd.format(new Date());
        excelFileName = "房态表"+date+".xlsx"; // 设置下载的文件名
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ new String((excelFileName).getBytes(), "iso-8859-1"));
        ServletOutputStream out = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(excelStream);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (final IOException e) {
            throw e;
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
    }
}
